package com.xu.base.model;

import com.xu.base.enums.ResponseCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 通用返回对象
 * @Author: 许乾伟
 * @Date: 2023/8/11 14:12
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseResponse<T> implements Serializable {

    /**
     * 返回状态码
     */
    private Integer code;
    /**
     * 返回消息
     */
    private String msg;
    /**
     * 返回数据
     */
    private T data;

    /**
     * 成功返回
     * @param t
     * @return
     * @param <T>
     */
    public static <T> BaseResponse<T> success(T t){
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(ResponseCodeEnum.SUCCESS.getCode());
        response.setMsg("success");
        response.setData(t);
        return response;
    }

    /**
     * 异常返回
     * @param message
     * @return
     */
    public static BaseResponse<Object> failed(String message){
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(ResponseCodeEnum.FAIL.getCode());
        response.setMsg(message);
        return response;
    }
}
